<template>
  <div class="app-container">
    <el-form
      ref="form"
      :inline="true"
      :model="formData"
      class="demo-form-inline"
    >
      <el-form-item>
        <el-select
          clearable
          filterable
          v-model="formData.shopId"
          placeholder="请选择店铺"
        >
          <el-option
            v-for="item in shopList"
            :label="item.shopName"
            :value="item.id"
            :key="item.id"
          ></el-option>
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-select
          clearable
          filterable
          v-model="formData.status"
          placeholder="状态"
        >
          <el-option label="上线" value="1"></el-option>
          <el-option label="下线" value="2"></el-option>
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="onSearch">搜索</el-button>
        <el-button type="primary" @click="reset">重置</el-button>
      </el-form-item>
    </el-form>

    <el-button class="create" type="primary" @click="create">创建</el-button>

    <el-table :data="list" style="width: 100%">
      <el-table-column prop="id" label="ID" width="180"> </el-table-column>

      <el-table-column prop="opBizCode" label="店铺" width="260">
        <template slot-scope="scope">
          {{
            shopListMap[scope.row.shopId]
              ? shopListMap[scope.row.shopId].shopName
              : "-"
          }}
        </template>
      </el-table-column>

      <el-table-column prop="province" label="区域"> </el-table-column>

      <el-table-column prop="status" label="状态">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.status == 1" type="success">上线</el-tag>
          <el-tag v-if="scope.row.status == 2" type="danger">下线</el-tag>
        </template>
      </el-table-column>

      <el-table-column prop="remark" label="备注"> </el-table-column>
      <el-table-column prop="updateBy" label="操作人"> </el-table-column>
      <el-table-column prop="updateTime" label="更新时间" width="180">
      </el-table-column>

      <el-table-column label="操作" width="180" fixed="right">
        <template slot-scope="scope">
          <el-button type="text" size="small" @click="edit(scope.row.id)"
            >编辑</el-button
          >

          <el-popconfirm
            v-if="scope.row.status == 2"
            style="margin-left: 10px"
            title="您确定要上线吗？"
            @confirm="updateStatus(scope.row, '1')"
          >
            <el-button type="text" size="small" slot="reference"
              >上线</el-button
            ></el-popconfirm
          >

          <el-popconfirm
            style="margin-left: 10px"
            title="您确定要下线吗？"
            v-if="scope.row.status == 1"
            @confirm="updateStatus(scope.row, '2')"
          >
            <el-button type="text" size="small" slot="reference"
              >下线</el-button
            ></el-popconfirm
          >

          <el-popconfirm
            style="margin-left: 10px"
            title="您确定要删除当前店铺吗？"
            @confirm="deleteItem(scope.row.id)"
          >
            <el-button type="text" size="small" slot="reference"
              >删除</el-button
            >
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="queryParams.total > 0"
      :total="queryParams.total"
      :page.sync="queryParams.page"
      :limit.sync="queryParams.pageSize"
      @pagination="getListData"
    />
  </div>
</template>

<script>
import {
  getAreaShopList,
  deleteAreaShop,
  editAreaShop,
} from "@/api/shop/index";
import { getShopList } from "@/api/shop/index";
import { codeToText } from "element-china-area-data";
export default {
  name: "Index",
  data() {
    return {
      codeToText,
      formData: {
        shopId: "",
        status: "",
      },
      queryParams: {
        page: 0,
        pageSize: 10,
        total: 0,
      },
      list: [],
      shopListMap: {},
      shopList: [],
    };
  },
  created() {
    this.getListData({
      page: 1,
      limit: 10,
    });

    getShopList({}, 1, 999999).then((res) => {
      let shopListMap = {};
      res.data.records.forEach((item) => {
        shopListMap[item.id] = item;
      });
      this.shopList = res.data.records;
      this.shopListMap = shopListMap;
    });
  },
  methods: {
    getListData({ page, limit }) {
      getAreaShopList(
        {
          shopName: this.formData.shopName,
          status: this.formData.status,
        },
        page || this.queryParams.page + 1,
        limit
      ).then(({ data }) => {
        this.list = data.records;
        this.queryParams.total = data.total;
        this.queryParams.page = data.current;
        this.queryParams.limit = data.size;
      });
    },

    onSearch() {
      this.queryParams = {
        ...this.queryParams,
        ...this.formData,
      };
      this.getListData({
        page: 1,
        limit: 10,
      });
    },

    reset() {
      this.formData = {
        shopId: "",
        status: "",
      };
      this.queryParams = {
        page: 0,
        pageSize: 10,
        total: 0,
      };
      this.getListData({
        page: 1,
        limit: 10,
      });
    },

    deleteItem(id) {
      deleteAreaShop(id).then((res) => {
        this.$message.success("删除成功");
        if (this.$store.state.user.currentShop.id === id) {
          window.location.reload();
        } else {
          this.getListData(this.queryParams);
        }
      });
    },
    create() {
      this.$router.push({ path: "/shop/dyOrder-area-edit" });
    },
    edit(id) {
      this.$router.push({ path: `/shop/dyOrder-area-edit?id=${id}` });
    },
    updateStatus(item, status) {
      editAreaShop({
        ...item,
        status,
      }).then((res) => {
        this.loading = false;
        this.$message.success("操作成功");
        this.getListData(this.queryParams);
      });
    },
  },
};
</script>

<style lang="scss" scoped>
.create {
  margin-bottom: 20px;
}
</style>
